package com.blogs.ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blogs.component.BaseActivity;
import com.blogs.entity.UserCenterInfo;
import com.blogs.service.GetUserCenter;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.ImageDownLoad;
import com.blogs.tools.Tools;
import com.cnblogs.R;

public class UserCenterActivity extends BaseActivity implements OnClickListener {
	private DBSharedPreferences sp;
	private LinearLayout uc_list_body;
	private TextView title_include_title;
	private Button title_include_back;
	private RelativeLayout title_top_bg;
	private RelativeLayout u_center_signout;
	private TextView u_center_nickname;

	private LinearLayout uc_avatar_bg_ll;
	private ImageView uc_avatar_iv;
	private TextView uc_blog_adress_tv;
	private TextView uc_followees_tv;
	private TextView uc_followers_tv;
	private TextView uc_register_time_tv;

	private RelativeLayout uc_blog_rl;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_cneter);
		sp = new DBSharedPreferences(this);
		if (sp.GetSessionID().equals("")) {
			this.goToLogin();
			this.finish();
		} else {
			ViewGroup headview = (ViewGroup) findViewById(R.id.main_title_include);
			title_include_title = (TextView) headview.findViewById(R.id.title_include_title);
			title_include_back = (Button) headview.findViewById(R.id.title_include_back);
			title_include_title.setText("用户中心");
			title_top_bg = (RelativeLayout) headview;
			title_include_back.setVisibility(View.VISIBLE);
			title_include_back.setOnClickListener(this);
			uc_list_body=(LinearLayout)findViewById(R.id.uc_list_body);
			
			uc_avatar_iv=(ImageView)findViewById(R.id.uc_avatar_iv);
			uc_avatar_bg_ll=(LinearLayout)findViewById(R.id.uc_avatar_bg_ll);
			u_center_signout = (RelativeLayout) findViewById(R.id.u_center_signout);
			u_center_nickname = (TextView) findViewById(R.id.u_center_nickname);
			uc_blog_adress_tv = (TextView) findViewById(R.id.uc_blog_adress_tv);
			uc_followees_tv = (TextView) findViewById(R.id.uc_followees_tv);
			uc_followers_tv = (TextView) findViewById(R.id.uc_followers_tv);
			uc_register_time_tv = (TextView) findViewById(R.id.uc_register_time_tv);
			uc_blog_rl=(RelativeLayout)findViewById(R.id.uc_blog_rl);
			uc_blog_rl.setOnClickListener(this);
			u_center_signout.setOnClickListener(this);
			u_center_nickname.setText(sp.GetNickName());
			Resources res=getResources();
			Bitmap avabmp=BitmapFactory.decodeResource(res, R.drawable.sample_face);
			uc_avatar_iv.setImageBitmap(Tools.toRoundCorner(avabmp, 5) );
			this.loadAvatar();
			this.initDialog();// 初始化对话框
			this.requestData();
			this.InitSkin();
		}
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			uc_list_body.getBackground().setLevel(2);
			uc_avatar_bg_ll.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			uc_list_body.getBackground().setLevel(1);
			uc_avatar_bg_ll.getBackground().setLevel(1);
		}
	}
	private void loadAvatar()
	{
		if(sp.equals(""))
			return;
		ImageDownLoad.loadDrawable(sp.GetUserAvatar(), "avatar_"+sp.GetUserID(), new ImageDownLoad.ImageCallBack() {
			
			public void onSuccess() {
				// TODO Auto-generated method stub
				
			}
			
			public void onSuccess(Bitmap bitmap) {
				// TODO Auto-generated method stub
				
				uc_avatar_iv.setImageBitmap(Tools.toRoundCorner(bitmap, 5) );
			}
			
			public void onFailed() {
				// TODO Auto-generated method stub
				
			}
		});
	}
	private void requestData() {
		dialog.show();
		this.requestUserInfo();
	}

	private void initDialog() {
		// 初始化对话框
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.setMessage("正在加载数据...");
	}

	public void requestUserInfo() {
		new GetUserCenter(sp.GetSessionID(), sp.GetUserID(), new GetUserCenter.IMsgCallBack() {

			public void onSuccess(UserCenterInfo author) {
				// TODO Auto-generated method stub
				uc_blog_adress_tv.setText(author.blog_address.equals("") ? "未开通" : author.blog_address);
				uc_followees_tv.setText(author.followees);
				uc_followers_tv.setText(author.followers);
				uc_register_time_tv.setText(author.register_time);

				dialog.hide();
			}

			public void onFailed() {
				// TODO Auto-generated method stub
				dialog.hide();
			}
		}).loadData();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == title_include_back) {
			this.finish();
		} else if (v == u_center_signout) {
			new AlertDialog.Builder(UserCenterActivity.this).setTitle("注销登录").setMessage("是否注销？")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							sp.SetSessionID("");
							sp.SetNickName("");
							sp.SetUserID("");
							sp.SetUserName("");
							sp.SetUserAvatar("");
							goToLogin();
							finish();
						}
					}).setNegativeButton("取消", null).show();

		}
		else if(v==uc_blog_rl)
		{
			if(uc_blog_adress_tv.getText().equals("")||uc_blog_adress_tv.getText().equals("未开通"))
				return;
			Intent intent_author=new Intent();
			intent_author.setClass(UserCenterActivity.this,AuthorHomePage.class);
			intent_author.putExtra("blogapp", sp.GetUserID());
			startActivity(intent_author);
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dialog.dismiss();
	}
	public void goToLogin() {

		Intent intent = new Intent();
		intent.setClass(this, UserLoginActivity.class);
		intent.putExtra("from", "com.blogs.ui.UserCenterActivity");
		startActivity(intent);
	}
}
